local xunyi = fk.CreateSkill{
  name = "hanqing__xunyi",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["hanqing__xunyi"] = "徇义",
  [":hanqing__xunyi"] = "限定技，其他角色进入濒死状态时，你可以与其交换体力值，然后你展示当前回合角色的手牌并回复其中"..
  "<font color='red'>♥</font>牌数的体力。",

  ["#hanqing__xunyi-invoke"] = "徇义：是否与 %dest 交换体力值？",
}

xunyi:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xunyi.name) and target ~= player and target.dying and
      player:usedSkillTimes(xunyi.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = xunyi.name,
      prompt = "#hanqing__xunyi-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.hp, target.hp = math.min(target.hp, player.maxHp), math.min(player.hp, target.maxHp)
    room:broadcastProperty(target, "hp")
    room:broadcastProperty(player, "hp")
    room:broadcastPlaySound("./audio/system/losehp")
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event == nil then return end
    local current = turn_event.data.who
    if not current.dead and not current:isKongcheng() then
      local n = #table.filter(current:getCardIds("h"), function (id)
        return Fk:getCardById(id).suit == Card.Heart
      end)
      current:showCards(current:getCardIds("h"))
      if player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = n,
          recoverBy = player,
          skillName = xunyi.name,
        }
      end
    end
    room.logic:getCurrentEvent():findParent(GameEvent.Dying, true):addCleaner(function()
      if not player.dead and player.hp < 1 then
        room:enterDying({
          who = player,
        })
      end
    end)
  end,
})

return xunyi
